
package com.insigma.business.qrcode.intell.service.impl;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.intell.dao.CommonQaInfoDao;
import com.insigma.business.qrcode.intell.dto.CommonQaInfoBSearcher;

import com.insigma.business.qrcode.intell.entity.CommonQaInfoDO;
import com.insigma.business.qrcode.intell.service.CommonQaInfoService;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("commonQaInfoService")
public class CommonQaInfoServiceImpl implements CommonQaInfoService {

    @Autowired(required = false)
    private CommonQaInfoDao commonQaInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<CommonQaInfoDO> search(final CommonQaInfoBSearcher commonQaInfoBSearcher) {
        if (Strings.isNotBlank(commonQaInfoBSearcher.getAdminAreaCode())) {
            commonQaInfoBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(commonQaInfoBSearcher.getAdminAreaCode()));
        }

        return commonQaInfoDao.search(commonQaInfoBSearcher);
    }


    @Override
    public CommonQaInfoDO get(final String id) {
        return commonQaInfoDao.get(id);
    }

    @Override
    public Integer insert(final CommonQaInfoDO commonQaInfoDO) {
        commonQaInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.COMMON_QA_INFO_ID));
        if (commonQaInfoDO.getSeqNo() != null && Strings.isNotBlank(commonQaInfoDO.getAdminAreaCode())) {

            CommonQaInfoDO byAreaAndSeq = commonQaInfoDao.getByAreaAndSeq(commonQaInfoDO.getAdminAreaCode(), commonQaInfoDO.getSeqNo());
            if (byAreaAndSeq != null) {
                commonQaInfoDao.addSeq(commonQaInfoDO.getAdminAreaCode(), commonQaInfoDO.getSeqNo());
            }
        }
        return commonQaInfoDao.insert(commonQaInfoDO);
    }


    @Override
    public Integer update(final CommonQaInfoDO commonQaInfoDO) {
        if (commonQaInfoDO.getSeqNo() != null && Strings.isNotBlank(commonQaInfoDO.getAdminAreaCode())) {

            CommonQaInfoDO byAreaAndSeq = commonQaInfoDao.getByAreaAndSeq(commonQaInfoDO.getAdminAreaCode(), commonQaInfoDO.getSeqNo());
            if (byAreaAndSeq != null && !byAreaAndSeq.getId().equals(commonQaInfoDO.getId())) {
                commonQaInfoDao.addSeq(commonQaInfoDO.getAdminAreaCode(), commonQaInfoDO.getSeqNo());
            }
        }
        return commonQaInfoDao.update(commonQaInfoDO);
    }


    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        CommonQaInfoDO commonQaInfoDO = get(id);
        if (commonQaInfoDO == null) {
            return 0;
        }
        commonQaInfoDao.subtractSeq(commonQaInfoDO.getAdminAreaCode(), commonQaInfoDO.getSeqNo());
        return commonQaInfoDao.delete(id, modifierType, modifierId);
    }



    @Override
    public Integer addHelpfulCount(String title, Boolean helpful) {
        return commonQaInfoDao.addHelpfulCount(title, helpful);

    }

    @Override
    public Integer addHelpfulCountById(String id, Boolean helpful) {
        return commonQaInfoDao.addHelpfulCountById(id, helpful);
    }


}
